package com.hellohoper.cloud.starter.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Created by hk on 17/8/19.
 */
@ConditionalOnClass({RedisTemplate.class})
@EnableCaching
public class RedisConfig {

    @Bean
    public KeyGenerator keyGenerator() {
        KeyGenerator keyGenerator = (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
        return keyGenerator;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //设置缓存过期时间
//        rcm.setDefaultExpiration(600);//秒
        //设置value的过期时间
//        Map<String, Long> map = new HashMap<String, Long>();
//        map.put("test", 60L);
//        rcm.setExpires(map);
        return rcm;
    }

//    @Bean
//    public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory) {
//        RedisTemplate<?, ?> template = new StringRedisTemplate(factory);
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        //如果key是String 需要配置一下StringSerializer,不然key会乱码 /XX/XX
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
//        return template;
//    }

    @Bean
    public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<?, ?> template = new StringRedisTemplate(factory);
        HsRedisSerializer hsRedisSerializer = new HsRedisSerializer();
        //如果key是String 需要配置一下StringSerializer,不然key会乱码 /XX/XX
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(hsRedisSerializer);
        template.setValueSerializer(hsRedisSerializer);
        template.setDefaultSerializer(hsRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
